package main

import (
	"log"
	"net/http"
)

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		query := req.URL.Query()
		a := query.Get("a")
		log.Println("a:", a)
		_, err := w.Write([]byte("hello there you are!"))
		if err != nil {
			log.Fatal("error to write!", err)
		}
	})

	//现在支持请求模式，以及路径变量了。as of 1.22
	mux.HandleFunc("GET /api/user/{id}", func(w http.ResponseWriter, req *http.Request) {
		id := req.PathValue("id")
		log.Println("id:", id)

		_, err := w.Write([]byte("hello there you are!"))
		if err != nil {
			log.Fatal("error to write!", err)
		}
	})

	err := http.ListenAndServe(":8888", mux)
	if err != nil {
		panic(err)
	}
}
